package hp.com.dao.impl;

import hp.com.dao.BaseDao;
import hp.com.dao.OrdersDAO;
import hp.com.entity.OrderDetail;
import hp.com.entity.Orders;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;

public class OrderDAOImpl extends BaseDao implements OrdersDAO {
    @Override
    public List<OrderDetail> GetOrderDetailsByOrder(String serial) throws Exception {
        String sql = "select * from order_detail where orderid=?";
        ResultSet rs = this.executeQuery(sql,new Object[]{serial});
        List<OrderDetail> list = new ArrayList<>();
        while(rs.next()){
            OrderDetail detail = new OrderDetail();
            detail.setId(rs.getInt("id"));
            detail.setOrderid(rs.getString("orderid"));
            detail.setGood(rs.getInt("good_id"));
            detail.setPic(rs.getString("pic"));
            detail.setAmount(rs.getInt("amount"));
            detail.setSingeCost(rs.getDouble("single_Cost"));
            detail.setStat(rs.getInt("stat"));
            detail.setName(rs.getString("good_Name"));
            detail.setSUK(rs.getString("SUK"));
            detail.setStatText(getShipTextByStat(detail.getStat()));
            list.add(detail);
        }
        return list;
    }
    @Override
    public Orders getOrderBySerial(String serial) throws Exception {
        String sql = "select * from orders where serialnumber = ?";
        ResultSet rs= this.executeQuery(sql,new Object[]{serial});
        rs.next();
        Orders order = new Orders();
        order.setSerial(rs.getString("serialnumber"));
        order.setUserAddress(rs.getString("useraddress"));
        order.setUserId(rs.getInt("userid"));
        order.setCreateTime(String.valueOf(rs.getDate("createtime")+" "+rs.getTime("createtime")));
        order.setCost(rs.getDouble("cost"));
        order.setDetail(GetOrderDetailsByOrder(order.getSerial()));
        order.setCount(CountOrderById(order.getSerial()));
        order.setStat(rs.getInt("status"));
        order.setStatText(getStatsTextByStat(order.getStat()));
        return order;
    }
    @Override
    public List<Orders> GetOrdersByUserId(int id) throws Exception {
        String sql = "select * from orders where userid = ? order by createtime desc";
        ResultSet rs = this.executeQuery(sql,new Object[]{id});
        List<Orders> orders = new ArrayList<>();
        while(rs.next()){
            Orders order = new Orders();
            order.setSerial(rs.getString("serialnumber"));
            order.setUserAddress(rs.getString("useraddress"));
            order.setUserId(rs.getInt("userid"));
            order.setCreateTime(String.valueOf(rs.getDate("createtime")+" "+rs.getTime("createtime")));
            order.setCost(rs.getDouble("cost"));
            order.setDetail(GetOrderDetailsByOrder(order.getSerial()));
            order.setCount(CountOrderById(order.getSerial()));
            order.setStat(rs.getInt("status"));
            order.setStatText(getStatsTextByStat(order.getStat()));
            orders.add(order);
        }
        return orders;
    }

    @Override
    public void DoInsertOrder(Orders order) throws Exception {
        String sql = "insert into orders (serialnumber,useraddress,userid,createtime,cost) values(?,?,?,?,?)";
        this.executeUpdate(sql,new Object[]{order.getSerial(),order.getUserAddress(),order.getUserId(),order.getCreateTime(),order.getCost()});
       List<OrderDetail> list=order.getDetail();
        String sqlDetail = "insert into order_detail (orderid,good_id,good_Name,pic,amount,single_Cost,SUK,stat) values(?,?,?,?,?,?,?,?)";
       for (int i = 0; i < list.size(); i++) {
            this.executeUpdate(sqlDetail,new Object[]{order.getSerial(),list.get(i).getGood(),list.get(i).getName(),list.get(i).getPic(),list.get(i).getAmount(),list.get(i).getSingeCost(),list.get(i).getSUK(),list.get(i).getStat()});
        }
    }

    @Override
    public int CountOrderById(String id) throws Exception {
        String sql ="select count(*) from order_detail where orderid = ?";
        ResultSet rs = this.executeQuery(sql,new Object[]{id});
        rs.next();
        return rs.getInt(1);
    }

    @Override
    public String getStatsTextByStat(int code) {
        switch (code){
            case -1:
                return "已取消";
            case 0:
                return "待支付";
            case 1:
                return "已支付";
        }
        return "";
    }

    @Override
    public String getShipTextByStat(int code) {
        switch (code){
            case -1:
                return "已取消";
            case 0:
                return "待付款";
            case 1:
                return "待发货";
            case 2:
                return "运输中";
            case 3:
                return "已签收";
        }
        return "";
    }
}
